
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
	function removecart(id){
		$.ajax({
			url:'cart-control.php?action=remove',
			type:'post',
			data: 'id='+id,
			beforeSend:function(){
				$('#more_popup').html('<div class="error" style="padding:30px;"><img  src="BootCommerce Demo_files/loader.gif" alt="Loading..." align="middle"/></div>');       
			},
			success:function(data){
	
				location.reload();
			}
		});
	}	
	function updatecart(id){
		var qty = $("#qty").val();
		$.ajax({
			url:'cart-control.php?action=update&qty='+qty,
			type:'post',
			data: 'id='+id,
			beforeSend:function(){
				$('#more_popup').html('<div class="error" style="padding:30px;"><img  src="BootCommerce Demo_files/loader.gif" alt="Loading..." align="middle"/></div>');       
			},
			success:function(data){
		
				location.reload();
			}
		});
	}
 </script>
<script type="text/javascript">

  $(document).ready(function($) {

   $('.footable').footable();
     
    var isAjaxReq = true;
      
   $('.qty-btn').on('click',function(e){
          var $this = $(this),
          $product_qty = $this.parent().parent().find('.product_qty');

          product_qty_val = Number($product_qty.val());

          if($this.hasClass('increment_qty')){
            $product_qty.val( product_qty_val + 1);
          }else{
            var newQtyVal = (product_qty_val - 1);
            $product_qty.val((newQtyVal <= 0) ? 1 : newQtyVal);
          };

      });


   $pageContainer = $('.main-container');
   $('.ajax-cart-form').on('submit',ajaxFormSubmit);

    $('.remove-item').on('click',removeItemRequest);

    $('.checkout_with_paypal').on('click', function(e){
         e.preventDefault();
        var $this = $(this);
         if($this.hasClass('disabled'))
         return false;

         window.location = $this.attr('href');
    });

    function ajaxFormSubmit(e){
        e.preventDefault();

         var $this = $(this),
            formAction = $this.attr('action');

             if($this.hasClass('disabled'))
             return false;

            $this.addClass('disabled').attr('disabled', 'disabled');

       $.post(formAction, $this.serialize(),function(data){
             updateCart(data);
       },'JSON');
       };

       function removeItemRequest(e){

       e.preventDefault();
          var $this = $(this),
           actionURL = $this.attr('href');

            $this.parents('tr').remove();

          $.post(actionURL, function(data){
             updateCart(data);
          },'JSON');
       };

       function updateCart(data){
            $('.cartContainer').replaceWith(data.page_data);

            $('.shopping-cart-btn').html(data.cartBtnMarkup).find('i.icon-shopping-cart').removeClass('icon-shopping-cart').addClass('glyphicon glyphicon-shopping-cart');
       };

    $('.checkout_submit_order').on('click', function(e){
        e.preventDefault();

        if ($('.cartContainer').data('backtocart') == 'cart') {
          isAjaxReq = false;
        }
        
        var $this = $(this);
        if($this.hasClass('disabled'))
        return false;
        $cartModal = isAjaxReq ? $('#cartModal') :  $('.cartContainer');

        $cartModal.finish().slideUp();
        
        if (isAjaxReq) {
          
          modalContent = 'modal-content';
          modalDialog  = 'modal-dialog';
          $('.cartContainer').data('backtocart','cart');

        } else {
          modalContent = '';
          modalDialog  = '';
          $('.cartContainer').data('backtocart','cart');
          
        }
        $cartModal.html('<div class="'+modalDialog+'"><div class="'+modalContent+'" id="cartContent"><div style="padding:10%; text-align:center;">  <img src="http://jqueryphpstoreshop-demo.livelyworks.net/1.2.0/assets/img/ajax-loader.gif"></br> Loading...</div></div></div>');
        $cartModal.slideDown();
        var $this = $(this),
            requestURL = $this.attr('href');
            $.post(requestURL, function(data){
              $cartModal.html('<div class="'+modalDialog+'"><div class="'+modalContent+'" id="cartContent">'+data.page_data+'</div></div>');
         
            },'JSON');
      });

  });
</script>
<?php 
session_start(); 
include("include/connect.php");
$html	.= "<div aria-hidden='false' style='display: block; overflow-y: scroll; width: auto; height: 94%;top:2%;' class='modal fade in' id='cartModal'> 
    <div class='modal-dialog'>
      <div class='modal-content' id='cartContent'>

<div class='cartContainer' data-backtocart=''>
  <form  class='form-horizontal ajax-cart-form' id='cart_form' style='margin:0;' method='post' accept-charset='utf-8'>
<input name='token_' id='cart_form_input' value='2c84cb74ec36719ef811eecc169999d5' style='display:none;' type='hidden'>
<div class='modal-header'>
           <button onclick='parent.closeIFrame()'  type='button' class='close' data-dismiss='modal' aria-hidden='true' title='Close Details'>x</button>
         <h3 id='myModalLabel'>Shopping Cart</h3>
  </div><br>
<div class='model-container table-responsive' id='ModelProductData'>
 <table id='cart_table' class='table table-striped table-bordered table-responsive'>
<thead>
<tr>
  <th data-toggle='true' width='20%'>Item Description</th>
  <th data-hide='phone,tablet' width='25%'>image</th>
  <th data-hide='phone' width='15%'>Price</th>
  <th data-hide='phone' width='15%'>Qty.</th>
  <th style='text-align:right;' width='15%'>Sub-Total</th>
  <th width='5%'>Remove</th>
</tr>
</thead>
<tbody>";
$total_number=0;
$total_price =0;
if(isset($_SESSION['cart']))
{
	foreach($_SESSION['cart'] as $cart_id => $x)
	{ 
	
		$total_number = $total_number + $x['qty'];	
		//$_SESSION['total_number'] = $total_number;
		$results = mysqli_query($con, "SELECT * from product WHERE ProductID=$cart_id");
		$result = mysqli_fetch_array($results);
		$PID1 = $result['ProductID'];
		$im = $result['SmallImage'];
		$total_price = $total_price + ($result['Price']* ($x['qty'] ? $x['qty'] : 1));
		if($PID1>12310)
		{
			$im= substr($im, 0, -4);
			$im="poster/".$im."_thumbnail.jpg";
		}
		else if($PID1<=12310)
		{
			$im="poster/".$im;
		}
		if($_SESSION['cart'][$cart_id]['qty'] == ''){ $qtyy =  '1';}else{$qtyy = $_SESSION['cart'][$cart_id]['qty'];}
		$prices = $result['Price']* $_SESSION['cart'][$cart_id]['qty'] ? $_SESSION['cart'][$cart_id]['qty'] : 1;
		$html .="<tr>
         <td>".$result['ProductName']."</td>
            
       
		<td><img class='lazy' data-original='http://q8movies.com/".$im."' src='http://q8movies.com/".$im."'  style='display: inline;height:200px;'></td>
                                        
                                  
          <td>".$result['Price']."</td>
          <td>
           
			<input type='number' readonly style='width:40px;' id='qty' value='".$qtyy."'>
	
         </td>
          <td style='text-align:right;'> ".$prices."></td>
          <td colspan='4'><a class='btn btn-default remove-item' onclick='removecart(".$result['ProductID'].");'><i class='glyphicon glyphicon-remove' style='color:red;'>X</i></a></td>

        </tr>";
	}
	}
 $_SESSION['total_number'] = $total_number;
 $_SESSION['subtotal'] = $total_price;                                                        
$html .="</tbody>

</table>
</div>
 
</form>

<div class='modal-footer'>
<div id='shopping_cart_total' class='alert alert-info' style='text-align:right;'>Base-Total: ".$_SESSION['subtotal']." + Vat: 4.23 = <strong>Total: ".$_SESSION['subtotal']+(4.23)."</strong>
<a href='cart.php' target='_blank'>View Cart</a>
</div>

  
	<a  onclick='parent.closeIFrame()' class='btn btn-default btn-sm cart-btn continue-shopping' data-dismiss='modal' aria-hidden='true' title='Continue Shopping'><i class='glyphicon glyphicon-arrow-left'></i>&nbsp;Continue Shopping</a>
  	<a  class='btn btn-warning btn-sm cart-btn checkout_submit_order '><i>Submit Order</i> <i class='glyphicon glyphicon-envelope'></i></a>
	 <a  class='btn btn-warning btn-sm cart-btn checkout_with_paypal '><i>Checkout with</i> <img src='img/logo_paypal_106x28.png'></a>
  
</div>

</div></div>
    </div>
  </div>";
  
  //returning data
	$data			=	array(
							'html'			=>	$html
							
						);
	//echo json_encode($data);
	echo $html; 
?>
